//
//  MainZeroController.m
//  project
//
//  Created by 张振宇 on 15/10/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainZeroController.h"
#import "PKRequestManager.h"
#import "MainCoModel.h"
#import "MainZeroCollectionViewCell.h"

@interface MainZeroController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong)UICollectionView *collection;
@end

@implementation MainZeroController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    CGFloat item = (kWidth - 30)/2;
    layout.itemSize = CGSizeMake(item, item + 80);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 5, kWidth, kHeight - 50) collectionViewLayout:layout];
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    
    [_collection registerClass:[MainZeroCollectionViewCell class] forCellWithReuseIdentifier:@"MainZeroCollectionViewCell"];
    
    [self.view addSubview:_collection];
    // Do any additional setup after loading the view.
}
- (void)requestData
{
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/brands/%@/items?limit=20&offset=0",_Myid];
    [PKRequestManager requestWithTypeP:GET urlString:str parDic:nil finish:^(NSData *data){
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        _dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *tabDic in dict[@"data"][@"items"]) {
            MainCoModel *model = [[MainCoModel alloc]init];
            [model setValuesForKeysWithDictionary:tabDic];
            [_dataSource addObject:model];
        };
        [self.collection reloadData];
    }error:nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainZeroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainZeroCollectionViewCell" forIndexPath:indexPath];
    MainCoModel *model = self.dataSource[indexPath.row];
    cell.configure = model;
    
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
